//
//  UserEarningRecordController.m
//  CheekiOS
//
//  Created by mac on 2017/6/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEarningRecordController.h"
#import "UserEarningRecordCell.h"

@interface UserEarningRecordController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UserCharm1ListModel *model;
@property (nonatomic, strong)NSMutableArray *dataSource;

@property (nonatomic, strong)UILabel *cumulative;

@property (nonatomic, assign)NSInteger page;
@property (nonatomic, assign)NSInteger rows;

@property (nonatomic, strong)NSNumber *UserTypehidden;

@end

@implementation UserEarningRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _rows = 10;
    [self setNavgationBarHidden];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 64)];
    bgView.backgroundColor = Colorbackground;
    [self.view addSubview:bgView];
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(22, 19.5, 44, 44)];
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置，现设为居左，默认的是居中
    
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 5.5, 0, 0);
    [btn addTarget:self action:@selector(backActionbtn) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat TopsegmentFloat ;
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        NSLog(@"ipad");
        TopsegmentFloat =0;
        
    }else{
        TopsegmentFloat = 16;
        
        NSLog(@"iphone or ipod");
    }
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, TopsegmentFloat, KScreenWidth, 44*SCREEN_WIDTH_RATIO47)];
    titleLab.text = @"收益明细";
    titleLab.font = [UIFont systemFontOfSize:18];
    titleLab.textColor = ColorI(0x2e353f);
    titleLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLab];
    [bgView addSubview:btn];

    
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 80)];
    backView.backgroundColor = [UIColor whiteColor];
    
    _cumulative = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 70)];
    _cumulative.textAlignment = NSTextAlignmentCenter;
    _cumulative.textColor = ColorI(0x5978b6);
    _cumulative.font =FontP(15);
    UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, _cumulative.bottom, KScreenWidth, 10)];
    linLab.backgroundColor = RGB(243, 243, 243);
    
    [backView addSubview:_cumulative];
    [backView addSubview:linLab];
    [self.view addSubview:backView];
    
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, bgView.bottom, KScreenWidth, KScreenHeight-49) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    _tableView.tableHeaderView = backView;
    
    [self requestgetCommonOpen];
}

- (void)backActionbtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requestgetCommonOpen{
    [HcardHttpmanager getCommonOpen:[HQHttpManager getUserId] success:^(id result) {
        if([result[@"code"] isEqualToString:@"ok"]){
            _UserTypehidden = result[@"res"][@"open"];
            [self requestDate];

            
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
}
- (void)requestDate{
    
    _dataSource = [NSMutableArray array];
    
    [HcardHttpmanager getCharm1ListWithUid:[HQHttpManager getUserId] withPage:_page withRows:_rows success:^(id result) {
        
        _dataSource = [UserCharm1ListModel mj_objectArrayWithKeyValuesArray:result[@"res"][@"charm1List"]];
        
        if ([_UserTypehidden isEqualToNumber:@1]) {
            if (result[@"total"]) {
                _cumulative.text = [NSString stringWithFormat:@"累计提现：%@元",result[@"total"]];
                
            }else{
                _cumulative.text = @"累计提现：0元";
            }
        }else{
            if (result[@"total"]) {
                _cumulative.text = [NSString stringWithFormat:@"累计收益：%@魅力值",result[@"total"]];
                
            }else{
                _cumulative.text = @"累计收益：0魅力值";
            }
        }


        
   

        [_tableView reloadData];

    } failure:^(NSError *error) {
        
    }];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UserEarningRecordCell *cell = [UserEarningRecordCell cellWithTableView:tableView indexPath:indexPath];
    _model = _dataSource[indexPath.row];
    cell.model = _model;
        
    return cell;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

@end
